﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CMTVision.Contracts;
using CMTVision.Model;
using System.Configuration;

namespace CMTVision.Services
{
    public class WidgetService:IWidgetService
    {

        #region Implement IWidgetService

        public bool AddWidget(int type, int index,string columnId, long templateId)
        {
            bool result =false;
            using (CMTWidgetsEntities entity = CMTVision.Common.EntitiesManager.LoadWidgetEntity())
            {
                var newWidget = new Widget()
                {
                    ColumnID = columnId,
                    OrderNo = index
                };
                var widgetType = (from wt in entity.WidgetTypes where wt.ID == type  select wt).FirstOrDefault();
                var template = (from t in entity.Templates where t.ID == templateId select t).FirstOrDefault();
                if (widgetType != null && template != null)
                {
                    newWidget.Template = template;
                    newWidget.WidgetType = widgetType;
                    entity.AddToWidgets(newWidget);

                    int affectedRows = entity.SaveChanges();

                    if (affectedRows > 0)
                    {
                        var afterInsertWidgets = (from w in entity.Widgets
                                                  where w.OrderNo >= index
                                                     && w.ColumnID == columnId
                                                     && w.Template.ID == templateId
                                                  orderby w.OrderNo
                                                  select w).ToList();
                        afterInsertWidgets.ForEach(w =>
                        {
                            w.OrderNo++;
                        });
                        result = true;
                    }                    
                }
            }
            return result;
        }

        public bool MoveWidget(int id, string fromColumnId, int fromIndex, string toColumnId, int toIndex)
        {
            bool result = false;
            using (CMTWidgetsEntities entity = CMTVision.Common.EntitiesManager.LoadWidgetEntity())
            {
                var widget = (from w in entity.Widgets where w.ID == id && w.ColumnID == fromColumnId && w.OrderNo == fromIndex select w).FirstOrDefault();
                if (widget != null)
                {
                    var fromWidgetList = (from w in entity.Widgets where w.OrderNo > fromIndex && w.ColumnID == fromColumnId && w.Template.ID == widget.Template.ID orderby w.OrderNo select w).ToList();
                    fromWidgetList.ForEach(w => w.OrderNo--);

                    widget.ColumnID = toColumnId;
                    widget.OrderNo = toIndex;

                    var toWidgetList = (from w in entity.Widgets where w.OrderNo >= toIndex && w.ColumnID == toColumnId && w.Template.ID == widget.Template.ID orderby w.OrderNo select w).ToList();
                    toWidgetList.ForEach(w => w.OrderNo++);

                    result = entity.SaveChanges() > 0;                    
                }
            }
            return result;
        }

        public bool RemoveWidget(int id)
        {
            bool result =false;
            using (CMTWidgetsEntities entity = CMTVision.Common.EntitiesManager.LoadWidgetEntity())
            {
                var widget = (from w in entity.Widgets where w.ID == id select w).FirstOrDefault();
                if (widget == null)
                {
                    result = true;
                }
                else
                {
                    entity.DeleteObject(widget);
                    result = entity.SaveChanges() > 0;
                }
            }
            return result;
        }

        #endregion
    }
}
